package com.ys.peony.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.utils.StringUtils;

/**
 * 弹框
 */
public class AlertTwoBtnDialog {

    private Context context;

    /**
     * 回调接口
     **/
    private CustomAlertDialogCallBack callback;

    /**
     * 标题视图
     **/
    private TextView titleView;

    /**
     * 警告框
     **/
    private Dialog dialog;

    /**
     * 提示消息视图
     **/
    private TextView tipTv;

    /**
     * 取消按钮
     **/
    private TextView cancelBtn;

    /**
     * 确认按钮
     **/
    private TextView sureBtn;


    /**
     * 重载的构造方法
     *
     * @param context
     * @param callback
     */
    public AlertTwoBtnDialog(Context context, CustomAlertDialogCallBack callback) {
        this(context, callback, -1);
    }

    public AlertTwoBtnDialog(Context context, CustomAlertDialogCallBack callback, int windowType) {
        // 1.接受上下文对象
        this.context = context;

        // 2.接受回调
        this.callback = callback;

        // 3.生成窗体
        dialog = new Dialog(this.context, R.style.mydialog);
        // 4.初始化视图
        initUIView();
        dialog.show();
    }

    /**
     * 初始化窗口视图
     */
    private void initUIView() {
        LayoutInflater factory=LayoutInflater.from(this.context);
        View view=factory.inflate(R.layout.dialog_alert_twobtn_layout, null);

        tipTv = (TextView) view.findViewById(R.id.tip_tv);
        cancelBtn = (TextView) view.findViewById(R.id.cancel_btn);
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onCancelBtnClicked();
                }
                dismiss();
            }
        });
        sureBtn = (TextView) view.findViewById(R.id.confirm_btn);
        sureBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onSureBtnClicked();
                }
                dismiss();
            }
        });

        //显示修改密码布局文件
        dialog.setContentView(view);

    }


    /**
     * 构造方法
     *
     * @param context
     */
    public AlertTwoBtnDialog(Context context) {
        this(context, null);
    }

    public void setTitle(int resId) {
        titleView.setText(resId);
        titleView.setVisibility(View.VISIBLE);
    }

    public void setTitle(String title) {
        titleView.setText(title);
        titleView.setVisibility(View.VISIBLE);
    }

    public void setMessage(int resId) {
        tipTv.setText(resId);
    }

    public void setMessage(String message) {
        tipTv.setText(message);
    }

    /**
     * 设置按钮
     *
     * @param text
     */
    public void setPositiveButton(String text) {
        sureBtn.setText(StringUtils.trimNull(text, "确定"));
    }

    /**
     * 设置按钮
     *
     * @param text
     */
    public void setNegativeButton(String text) {
        cancelBtn.setText(StringUtils.trimNull(text, "取消"));
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        dialog.dismiss();
    }

    /*
     * 获取当前dialog
     */
    public Dialog getDialog() {
        return this.dialog;
    }
}
